<template>
  <div class="accountList">
    <div class="title">商品列表</div>
    <div class="operation">
      <el-button @click="dialogFormVisible = true">添加商品</el-button>
    </div>

    <el-table :data="logRecords" style="width: 100%">
      <el-table-column label="商品名称" prop="title"> </el-table-column>
      <el-table-column label="商品图片" prop="ctime">
        <template slot-scope="scope">
          <img :src="scope.row.imgUrl" class="coachImg">
        </template>
      </el-table-column>
      <el-table-column label="所需积分" prop="integral"> </el-table-column>
      <el-table-column label="商品简介" prop="jianjie"> </el-table-column>
      <el-table-column label="商品描述" prop="contents"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
          <el-button type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.currentPage"
      :page-sizes="[10, 20, 30, 40]" :page-size="this.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="this.total" background class="turnPage">
    </el-pagination>

    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-position="right" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload class="avatar-uploader" action="fakeaction" :show-file-list="false" :http-request="uploadFile"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.imgurl" :src="form.imgurl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="所需积分">
          <el-input v-model.number="form.integral"></el-input>
        </el-form-item>
        <el-form-item label="商品简介">
          <el-input v-model="form.jianjie"></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="form.contents"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMall">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑商品" :visible.sync="uploadDialog">
      <el-form :model="uploadform" label-position="right" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="uploadform.title"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload class="avatar-uploader" action="fakeaction" :show-file-list="false" :http-request="uploadFile2"
            :before-upload="beforeAvatarUpload">
            <img v-if="uploadform.imgUrl" :src="uploadform.imgUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="所需积分">
          <el-input v-model.number="uploadform.integral"></el-input>
        </el-form-item>
        <el-form-item label="商品简介">
          <el-input v-model="uploadform.jianjie"></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="uploadform.contents"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="uploadDialog = false">取 消</el-button>
        <el-button type="primary" @click="editMall">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getMallList, addMall, delMall, editMall, uploadFile } from "@/api/getData";
export default {
  //声明数据源
  data() {
    return {
      total: 0,
      currentPage: 1,
      pageSize: 10,
      logRecords: [],
      dialogFormVisible: false,
      uploadDialog: false,
      form: {
        title: '',
        contents: '',
        jianjie: '',
        imgurl: '',
        integral: null
      },
      uploadform: {
        title: '',
        contents: '',
        jianjie: '',
        imgUrl: '',
        integral: null,
        id: null
      },
    };
  },
  //定义方法
  methods: {
    uploadFile2(params) {
      const file = params.file;
      // 使用FormData传参数和文件
      var param = new FormData();
      // 文件
      param.append("file", file);
      uploadFile(param).then((res) => {
        this.uploadform.imgUrl = res.data
      });
    },
    uploadFile(params) {
      const file = params.file;
      // 使用FormData传参数和文件
      var param = new FormData();
      // 文件
      param.append("file", file);
      uploadFile(param).then((res) => {
        this.form.imgurl = res.data
      });
    },
    addMall() {
      addMall(this.form).then((res) => {
        if (res.code == 0) {
          this.$message.success('添加成功');
          this.dialogFormVisible = false
          this.getMallList()
        } else {
          this.$message.error('添加失败');
        }
      });
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    handleEdit(index, row) {
      this.uploadDialog = true
      this.uploadform.id = row.id
      this.uploadform.title = row.title
      this.uploadform.contents = row.contents
      this.uploadform.jianjie = row.jianjie
      this.uploadform.integral = row.integral
      this.uploadform.imgUrl = row.imgUrl
    },
    editMall() {
      editMall(this.uploadform).then((res) => {
        this.$message.success('修改成功');
        this.uploadDialog = false
        this.getMallList()
      });
    },
    handleDelete(index, row) {
      this.$confirm("确认删除该商品吗?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delMall(row.id).then((res) => {
          this.$message.success('删除成功');
          this.getMallList()
        });
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getMallList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getMallList();
    },

    getMallList() {
      const params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize
      };
      getMallList(params).then((res) => {
        this.logRecords = res.data.list
        this.total = res.data.total
      });
    }
  },
  mounted() {
    this.getMallList()
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #d9d9d9;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.accountList ::v-deep {
  .el-autocomplete {
    width: 174px;
    height: 42px;
    margin-right: 20px;
  }

  .el-select {
    width: 136px;
    height: 42px;
    margin-right: 20px;
  }

  .el-date-editor {
    width: 180px;
    height: 42px;
    margin-right: 20px;
  }
}

.accountList {
  padding: 0 56px;

  .coachImg {
    width: 100px;
    height: 100px;
  }

  .title {
    font-size: 20px;
    color: #3c3c3c;
    margin: 40px 0;
  }

  .operation {
    display: flex;
    justify-content: flex-start;
    margin: 20px 0;

    .operationBtn {
      width: 98px;
      height: 39px;
      background: #0c9a8d;
      color: #fff;
      font-size: 18px;
      padding: 0;
    }
  }

  .turnPage {
    text-align: right;
    margin-top: 20px;
  }

  .el-table::v-deep {

    thead th:first-child,
    tbody td:first-child {
      padding-left: 40px;
    }
  }
}
</style>